package com.moyq5.permit.center.web;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.permit.center.bean.entity.SysAdmin;
import com.moyq5.permit.center.feign.client.SysAdminFeign;

/**
 * 账号、密码Realm
 * @author Moyq5
 * @date 2019年8月13日
 */
@Component
public class ShiroLoginRealm extends ShiroRealm {
	
	@Autowired
	private SysAdminFeign adminFeign;
	
	@Override
	protected SysAdmin doGetAdminInfo(AuthenticationToken token) {
		SysAdmin admin = new SysAdmin();
		admin.setName((String)token.getPrincipal());
		admin.setPwd((String)token.getCredentials());
		ObjectResult<SysAdmin> objRst = adminFeign.login(admin);
		if (objRst.getStatus() != Status.OK) {
			throw new AuthenticationException(objRst.getMessage());
		}
		return objRst.getData();
	}
	
	@Override
	public Class<?> getAuthenticationTokenClass() {
		return ShiroLoginToken.class;
	}

}
